package view.userinteface;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import view.controller.ControllerPrincipal;

import beans.BeanProveedor;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class JInternalFrameAltaProveedor extends javax.swing.JInternalFrame {
	private static final long serialVersionUID = 1L;
	private JTextField txtRazonSocial;
	private JButton btnCancelar;
	private JButton btnAceptar;
	private JLabel lblNombre;
	private JLabel lblCUIT;
	private JLabel lblDNI;
	private JLabel lblTelefono;
	private JTextField txtCUIT;
	private JTextField txtDomicilio;
	private JTextField txtTelefono;
	private static JInternalFrameAltaProveedor instancia;


	public static JInternalFrameAltaProveedor getInstancia(){
		if (instancia==null){
			instancia = new JInternalFrameAltaProveedor();
		}
		return instancia;
	}
	
	public JInternalFrameAltaProveedor() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(400, 236));
			this.setBounds(0, 0, 400, 236);
			setVisible(true);
			this.setTitle("Alta Proveedor");
			getContentPane().setLayout(null);
			{
				txtTelefono = new JTextField();
				getContentPane().add(txtTelefono);
				txtTelefono.setBounds(175, 111, 166, 21);
			}
			{
				txtDomicilio = new JTextField();
				getContentPane().add(txtDomicilio);
				txtDomicilio.setBounds(175, 82, 166, 21);
			}
			{
				txtCUIT = new JTextField();
				getContentPane().add(txtCUIT);
				txtCUIT.setBounds(175, 19, 166, 21);
			}
			{
				txtRazonSocial = new JTextField();
				getContentPane().add(txtRazonSocial);
				txtRazonSocial.setBounds(175, 49, 166, 21);
			}
			{
				lblTelefono = new JLabel();
				getContentPane().add(lblTelefono);
				lblTelefono.setText("Telefono: ");
				lblTelefono.setBounds(55, 115, 120, 14);
			}
			{
				lblDNI = new JLabel();
				getContentPane().add(lblDNI);
				lblDNI.setText("Domicilio: ");
				lblDNI.setBounds(55, 85, 120, 14);
			}
			{
				lblCUIT = new JLabel();
				getContentPane().add(lblCUIT);
				lblCUIT.setText("CUIT: ");
				lblCUIT.setBounds(55, 23, 120, 14);
			}
			{
				lblNombre = new JLabel();
				getContentPane().add(lblNombre);
				lblNombre.setText("Razon social: ");
				lblNombre.setBounds(55, 52, 120, 14);
			}
			{
				btnAceptar = new JButton();
				getContentPane().add(btnAceptar, "Center");
				btnAceptar.setText("Aceptar");
				btnAceptar.setBounds(72, 159, 98, 26);
				btnAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						if(validar()){
							BeanProveedor beanProveedor = new BeanProveedor();
							beanProveedor.setCuit(txtCUIT.getText());
							beanProveedor.setDireccion(txtDomicilio.getText());
							beanProveedor.setRazonSocial(txtRazonSocial.getText());
							beanProveedor.setTelefono(txtTelefono.getText());
							if(ControllerPrincipal.getInstancia().altaProveedor(beanProveedor)){
								JOptionPane.showMessageDialog(JInternalFrameAltaProveedor.getInstancia(),"Se creo el proveedor");
								dispose();
							}else{
								JOptionPane.showMessageDialog(JInternalFrameAltaProveedor.getInstancia(),"Error al crear el proveedor. Contacte al administrador");
							}
							
						}else{
							JOptionPane.showMessageDialog(JInternalFrameAltaProveedor.getInstancia(),"Todos los campos deben estar completos");
						}
					}

				});
			}
			{
				btnCancelar = new JButton();
				getContentPane().add(btnCancelar);
				btnCancelar.setText("Cancelar");
				btnCancelar.setBounds(208, 159, 107, 26);
				btnCancelar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						dispose();
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private boolean validar() {
		if("".equalsIgnoreCase(txtCUIT.getText()) || "".equalsIgnoreCase(txtDomicilio.getText()) || "".equalsIgnoreCase(txtRazonSocial.getText())
				|| "".equalsIgnoreCase(txtTelefono.getText()))
			return false;
		else
			return true;
	}

}
